package com.shyfu.dao;

import com.shyfu.entities.Role;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Svitlana on 04-Apr-14.
 */
@Repository
public class RoleDAOImpl implements RoleDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addRole(Role role) {
        sessionFactory.getCurrentSession().save(role);
    }

    @Override
    public void removeRole(Role role) {
        sessionFactory.getCurrentSession().delete(role);
    }

    @Override
    public void removeRole(long id) {
        Role role = getRole(id);
        if (role != null) {
            removeRole(role);
        }
    }

    @Override
    public List<Role> getAllRoles() {
        return sessionFactory.getCurrentSession().createQuery("from Role order by authority").list();
    }

    @Override
    public Role getRole(long id) {
        return (Role) sessionFactory.getCurrentSession().load(Role.class, id);
    }
}
